var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({

    data: {
        orderGoodsList:[],
        goods_type:0,
        totalMoeny:0,
        totalNums:0,
        totalPoint:0,
        usePoint:0,
        defaultAddr:[],
        addressId:0,
        note:"",
        user_point:0,
        img_url: c.img_url,
        is_checked:false
    },
    onLoad: function (options) {
        var goodsExt = JSON.parse(options.goodsExt);
        var usePoint = goodsExt.totalPoint;
        if (usePoint>wx.getStorageSync("userinfo").money_dj) {
            usePoint = wx.getStorageSync("userinfo").money_dj;
        }
        this.setData({
            totalMoeny: goodsExt.totalMoney,
            totalPoint: 0,
            usePoint: usePoint,
            totalNums: goodsExt.totalNums,
            goods_type: goodsExt.goods_type,
            orderGoodsList: JSON.parse(options.orderGoods),
            user_point:wx.getStorageSync("userinfo").money_dj
        });
    },
    onShow:function(){
        this.getDefaultAddr();
        utils.updateUserinfo();
    },
    getDefaultAddr:function(){//获取默认地址
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
            url: "/v1/Useraddress/getdefault",
            token: token,
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                if (data.id == undefined){
                    that.setData({
                        defaultAddr: data,
                        addressId: 0
                    });
                }else{
                    that.setData({
                        defaultAddr: data,
                        addressId: data.id
                    });
                }
            } else {

            }
        });
    },
    editIsuseponit:function(e){
        var checked = e.currentTarget.dataset.check;
        if (!checked){
            if (this.data.user_point > this.data.usePoint){//如果用户积分大于抵扣积分
                var totalMoeny = parseFloat(this.data.totalMoeny) - parseFloat(this.data.usePoint);
            }else{
                var totalMoeny = parseFloat(this.data.totalMoeny) - parseFloat(this.data.user_point);
            }
        }else{
            if (this.data.user_point > this.data.usePoint) {//如果用户积分大于抵扣积分
                var totalMoeny = parseFloat(this.data.totalMoeny) + parseFloat(this.data.usePoint);
            } else {
                var totalMoeny = parseFloat(this.data.totalMoeny) + parseFloat(this.data.user_point);

            }
        }
        this.setData({
            is_checked: !checked,
            totalMoeny: totalMoeny.toFixed(2),
            totalPoint: checked ? 0  : this.data.usePoint
        })
    },
    editNote:function(e){
        this.setData({
            note:e.detail.value
        })
    }, 
    submit:function(){//提交创建订单
        if (this.data.addressId == 0 ){
            utils.errMsg("请先选择一个地址");
            return false;
        }
        var token = wx.getStorageSync("userinfo").wap_token;
        var goods_ids = "";
        var data = this.data.orderGoodsList;
        data.forEach((item, index) => {
            goods_ids += item.id + "|" + item.nums+",";
        })
        goods_ids = goods_ids.substr(0, goods_ids.length - 1);
        var userinfo = wx.getStorageSync("userinfo");
        var pay_money = this.data.totalMoeny - userinfo['money'];
        pay_money = pay_money > 0 ? pay_money : 0;
        var data = {
            url: "/v1/ordergoods/add",
            token: token,
            goods_price_ids: goods_ids,
            openid:wx.getStorageSync("openId"),
            address_id: this.data.addressId,
            pay_money: pay_money,
            pay_type:0,
            goods_type: this.data.goods_type,
            note:this.data.note,
            totalPoint: this.data.totalPoint,
            isUsepoints: this.data.is_checked
        };
        utils.request(data).then(function (res) {
          console.log(res);
            if (res.code == 0) {
                //直接支付成功!
                if (res.data.length == 0) {
                    utils.sucMsg("支付成功");
                    setTimeout(function () {
                        wx.redirectTo({
                            url: '/pages/shop/ordergoods/index',
                        })
                    }, 2000);
                    return false;
                }
                //走在线支付
                wx.requestPayment({
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr,
                    'package': res.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.paySign,
                    'success': function (res) {
                        utils.sucMsg("支付成功");
                        setTimeout(function () {
                            wx.redirectTo({
                                url: '/pages/shop/ordergoods/index',
                            })
                        }, 2000);
                    },
                    'fail': function (res) {
                        utils.errMsg("支付失败");
                        setTimeout(function () {
                            wx.redirectTo({
                                url: '/pages/shop/ordergoods/index',
                            })
                        }, 2000);
                    }
                })
            } else {
                utils.errMsg(res.msg);
            }
        });
    }
})